package com.longtop.perform.khyg.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.DateUtil;
import com.longtop.framework.util.StringUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.perform.khyg.model.TKhygIndex;
import com.longtop.perform.khyg.model.TKhygProject;
import com.longtop.perform.khyg.model.TKhygSegment;
import com.longtop.perform.khyg.service.ProjectService;

/**
 * 
 * @author 孙景弘
 * 
 */
public class ProjectManageAction extends BaseAction {

	private ProjectService service;
	private final String DATASET_PROJECT = "Dataset_project";// 考核方案
	private final String DATASET_INDEXSYS = "Dataset_indexSys";// 分段方式id对应名称
	private final String DATASET_PORJECT_INDEX = "Dataset_project_index";// 考核方案指标权重
	private final String DATASET_PROJECT_SEGMENT = "Dataset_project_segment";// 考核阶段

	// 查询条件：
	Map<String, Object> queryMap = new HashMap<String, Object>();

	public ProjectManageAction() {
		this.service = this.getBean(ProjectService.BEAN_ID);
	}

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		// 判断DATASET是否存在：
		Dataset ds_project = this.getDataset(context, DATASET_PROJECT);
		if (!WebUtil.validateDatasetExist(this, ds_project, DATASET_PROJECT))
			return;

		String projectId = this.getParameter(context, "projectId");
		if (!StringUtil.isEmpty(projectId)) {// 判断是新增还是编辑
			// 编辑：加载数据
			queryMap.put("projectId", projectId);
			service.queryForPageByIbatis("khygProjectSql.queryProject", null, queryMap, ds_project, false);
		}

	}

	/**
	 * 装载页面所需数据：指标id对应名称 ，分段方式id对应名称,指标权重
	 * 
	 */
	public void loadPageData(IActionContext context) {
		// 加载数据：
		Dataset ds_indexSys = this.getDataset(context, DATASET_INDEXSYS);
		Dataset ds_project_index = this.getDataset(context, DATASET_PORJECT_INDEX);
		if (!WebUtil.validateDatasetExist(this, ds_project_index, DATASET_PORJECT_INDEX) || !WebUtil.validateDatasetExist(this, ds_indexSys, DATASET_INDEXSYS))
			return;
		// 指标体系id对应名称（员工）
		queryMap.put("fitObject", "1");
		service.queryForPageByIbatis("khygProjectSql.indexSysIdToName", null, queryMap, ds_indexSys, false);
		// 方案-指标：
		String projectId = this.getParameter(context, "projectId");
		if (!StringUtil.isEmpty(projectId)) {
			queryMap.put("projectId", projectId);
			service.queryForPageByIbatis("khygProjectSql.projectIndexIdToName", null, queryMap, ds_project_index, false);
		}
	}

	/**
	 * 根据选择分段方式显示分段周期
	 * 
	 * @param context
	 */
	public void querySegment(IActionContext context) {
		Dataset ds_segment = this.getDataset(context, DATASET_PROJECT_SEGMENT);
		if (!WebUtil.validateDatasetExist(this, ds_segment, DATASET_PROJECT_SEGMENT))
			return;

		String cycUnit = this.getParameter(context, "cycleUnit");// 分段方式id：即周期单位
		String start = this.getParameter(context, "startDate");
		String end = this.getParameter(context, "endDate");
		// 转成date类型：
		Date startDate = DateUtil.parseDate(start);
		Date endDate = DateUtil.parseDate(end);
		queryMap.put("cycleUnit", cycUnit);
		queryMap.put("startDate", startDate);
		queryMap.put("endDate", endDate);

		List list = service.queryForListByIbatis("khygProjectSql.queryCycles", queryMap);
		BeanUtil.listToDataset(list, ds_segment, false);
	}

	/**
	 * 选择考核周期-根据选择分段方式显示分段周期
	 * 
	 * @param context
	 */
	public void queryProjectCycle(IActionContext context) {
		Dataset dataset = this.getDataset(context, "Dataset_cycle");// 选择考核周期
		if (!WebUtil.validateDatasetExist(this, dataset, "Dataset_cycle"))
			return;
		String cycUnit = this.getParameter(context, "cycleUnit");// 分段方式id
		String day = this.getParameter(context, "closedDay");// 临近日期
		Date closeDay = DateUtil.parseDate(day);
		queryMap.put("cycleUnit", cycUnit);
		queryMap.put("closeDay", closeDay);

		List list = service.queryForListByIbatis("khygProjectSql.queryProjectCycle", queryMap);
		BeanUtil.listToDataset(list, dataset, false);
	}

	/**
	 * 保存考核方案（包括方案分段，与方案对应的指标）
	 * 
	 */
	public void saveProject(IActionContext context) {
		String task = this.getParameter(context, "task");// 新增or修改
		Dataset ds_project = this.getDataset(context, DATASET_PROJECT);
		Dataset ds_project_index = this.getDataset(context, DATASET_PORJECT_INDEX);
		Dataset ds_project_segment = this.getDataset(context, DATASET_PROJECT_SEGMENT);
		try {
			// 考核方案，考核方案对应指标，考核方案分段：
			TKhygProject project = BeanUtil.datasetToVO(ds_project, TKhygProject.class);
			List projectIndexs = BeanUtil.datasetToList(ds_project_index, TKhygIndex.class, "indexName");
			List projectSegment = BeanUtil.datasetToList(ds_project_segment, TKhygSegment.class);

			SessionObject sessionObject = this.getSessionObject(context);
			service.saveProject(project, projectIndexs, projectSegment, task, sessionObject);
			// 返回保存后的方案：
			ds_project.clear();
			ds_project.appendObject(project);
			this.hintInfoMessage("保存方案成功。");
		} catch (BaseCheckedException e) {
			this.hintErrorMessage(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			this.hintErrorMessage("保存方案失败。");
		}
	}

	/**
	 * 判断方案是否存在定性考核指标
	 * 
	 * @param context
	 */
	public void hasDxIndex(IActionContext context) {
		String projectId = this.getParameter(context, "projectId");
		Integer count = (Integer) service.queryForObjectByIbatis("khygProjectSql.projectDxIndexCount", projectId);
		if (count == 0) {
			context.addCommandParameter("hasDxIndex", "false");
		} else {
			context.addCommandParameter("hasDxIndex", "true");
		}
	}

	/**
	 * 获取考核阶段对应指定步骤的状态
	 * 
	 * @param context
	 */
	public void getSegmentStatusByStep(IActionContext context) {
		// 传入参数：
		String segmentId = this.getParameter(context, "segmentId");
		int step = Integer.valueOf(this.getParameter(context, "step"));

		boolean flag = service.getSegmentStatus(TKhygSegment.class, segmentId, step);
		String flagStr = flag ? "true" : "false";
		context.addCommandParameter("stepStatus", flagStr);
	}
}
